/*
 * Gallery.cpp
 *
 *  Created on: 4/06/2013
 *      Author: roger
 */

#include "Gallery.h"

static const char *gallery_bitmap PROGMEM =
		"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
		"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
		"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
		"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
		"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
		"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
		"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
		"!!!!!!!!!!!!!!!!!!!!!!!!!1%\"!Q-$\"!1%\"!1%!!!!!!!!!!!!!!!!!!!!!A)#"
		"\"!1%!!!!!1%\"!A)#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
		"!!!!!!!!!1%\"!!!!#AI+86V>FZ?8O,CYOLK[O\\O\\J[?HDY_0:76F,S]P!!!!!!!!"
		"!!!!!1%\"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1%\"!1%\"$!Q-D)S-"
		"`````````````P\\_`@X^`P\\_`P\\_`0T]````````^P<WD9W.)#!A!!!!!A)#!1%\""
		"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!+SML`0T]`@X^`@X^^@8VQM,#X^`@"
		"_`P\\`@X^\\?XNPL[_\\_`P`P\\_````````````AI+#!Q-$\"!1%!!!!!!!!!!!!!!!!"
		"!!!!!1%\"!!!!!1%\"-4%R`````0T]`````0T]6V>8!1%\"!!!!]0$Q_PL[$1U.\"!1%"
		"+CIK````````````_`P\\````RM<'!!!!!!!!!!!!!!!!!!!!!!!!!!!!\"15&!!!!"
		"]@(R`P\\_````````````3UN,!Q-$!!!!\\O\\O^`@X\"Q=(!Q-$*35F````````````"
		"````_`P\\````N,3U!1%\"\"!1%!1%\"!!!!!!!!!!!!!Q-$L[_P````````````````"
		"````_@HZE*#1O\\O\\`````0T]V^@8C)C)YO,C_`P\\[_PL&R=8#1E*R-4%`@X^`@X^"
		"'BI;!1%\"!1%\"!!!!!!!!!!!!\"!1%^P<W`@X^N\\?X$AY/5F*3`0T]````````````"
		"`````````````````````P\\_SML+!A)#\"!1%?8FZ````````Q]0$!1%\"!1%\"!!!!"
		"!!!!!!!!\"A9'_@HZ````0DY_\"!1%\"!1%_@HZ````````````````````````````"
		"````````^`@X0$Q]*S=HYO,C````````\\?XN!Q-$!!!!!!!!!!!!!!!!#!A)]P,S"
		"`@X^HJ[?\"Q=(,#QM`0T]````````````````````````````````````````````"
		"````````````````^`@X\"A9'!1%\"!!!!!!!!!!!!!A)#P<W^`0T]`P\\_^@8V`0T]"
		"_`P\\````````````````````````````````````````````````````````````"
		"^`@X\"A9'!1%\"!!!!!!!!!!!!!1%\"\"!1%T]`0`````````````````0T]`P\\_`@X^"
		"`P\\_`````0T]````````````````````````````````````]`0T#1E*!!!!!!!!"
		"!!!!!!!!\"!1%!A)#!!!!/$AY66666V>8D9W._@HZ`@X^`P\\_`P\\_````````````"
		"````````````````````````````````\\?XN!A)#!1%\"!!!!!!!!!!!!!A)#!Q-$"
		"!A)#!1%\"!A)#!!!!!!!!7VN<`````P\\_R-4%!!!!!!!!RM<'````````````````"
		"````````````````=(\"Q!1%\"!1%\"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
		"!!!!!!!!````````,CYO!!!!!1%\"+CIK`````````````````````````0T]X>X>"
		"!!!!!1%\"!1%\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!!!!!!!!!!!!!!!!!!!!!!!!"
		"";

Gallery::Gallery() :
		App() {
	// Keep the constructor empty and do most things in the init()
}
void Gallery::init() {
#ifdef GALLERY_DEBUG
	Serial.print(getName());Serial.println(INIT);
#endif
	m_icon = new Icon(28, &gallery_bitmap[0]);
}
void Gallery::setup() {
	Graphics.setRotation((uint8_t)0);
	Graphics.fillScreen((uint16_t)WHITE);
	if (m_root) {
		m_root.close();
	}
	m_root = SD.open(PSTR("/"));
	if (!m_root) {
#ifdef GALLERY_DEBUG
		Serial.println(PSTR("Gallery::setup() failed to open root"));
#endif
		return;
	}
	m_root.rewindDirectory();
	char *n = findNextFile();
	if (n != NULL) {
		imgDraw(n,0,0);
	}
}

boolean Gallery::touch(Point p) {
	if (p.x > 270) {
		return true;
	}
	char *n = findNextFile();
	if (n != NULL) {
		imgDraw(n,0,0);
		return true;
	}
	return false;
}

char *Gallery::findNextFile() {
	while (true) {
		File entry = m_root.openNextFile();
		if (!entry) {
			// no more files
#ifdef GALLERY_DEBUG
			Serial.println(PSTR("Gallery::findNextFile: no more files"));
#endif
			break;
		}
		if (entry.isDirectory()) {
#ifdef GALLERY_DEBUG
			Serial.print(PSTR("Gallery::findNextFile:#3 "));
			Serial.println(entry.name());
#endif
			entry.close();
			continue;
		}
		char *pch = NULL;
#ifdef GALLERY_DEBUG
		Serial.print(PSTR("Gallery::findNextFile:#1 "));
		Serial.println(entry.name());
#endif
		pch = strstr (entry.name(),".BMP");
		if (pch == NULL) {
			pch = strstr (entry.name(),".TFT");
		}
		if (pch != NULL) {
#ifdef GALLERY_DEBUG
			Serial.println(entry.name());
			Serial.print(PSTR("Gallery::findNextFile: selected: "));
			Serial.println(entry.name());
#endif
			strcpy(m_lastName,entry.name());
			entry.close();
			return &m_lastName[0];
		}
#ifdef GALLERY_DEBUG
		Serial.print(PSTR("Gallery::findNextFile:#2 "));
		Serial.println(entry.name());
#endif
		entry.close();
	}
#ifdef GALLERY_DEBUG
	Serial.println(PSTR("Gallery::findNextFile: failed to find a file"));
#endif
	return NULL;
}

void Gallery::display() {
}

void Gallery::imgDraw(char *filename, int x, int y) {

	File imgFile;
	boolean goodBmp = false; // Set to true on valid header parse

	Graphics.fillScreen(BLACK);
	Graphics.setRotation(0);
	if ((x >= Graphics.width()) || (y >= Graphics.height()))
		return;
#ifdef GALLERY_DEBUG
	Serial.println();
	Hardware.progmemPrint(PSTR("Loading image '"));
	Serial.print(filename);
	Serial.println('\'');
#endif
	// Open requested file on SD card
	imgFile = SD.open(filename);
	if (!imgFile) {
#ifdef GALLERY_DEBUG
		Hardware.progmemPrintln(PSTR("File not found"));
#endif
		Graphics.setCursor(20, Graphics.height() / 2);
		Graphics.print(PSTR("File not found: "));
		Graphics.print(filename);
		return;
	}
	Graphics.setCursor(20, Graphics.height() / 2);
	Graphics.print(PSTR("Loading: "));
	Graphics.print(filename);
	File *m_imgFile = &imgFile;
	uint16_t signature = Hardware.read16(imgFile);
	ImageDisplay *imageDisplay = NULL;

	// Parse BMP header
	if (signature == 0x4D42) { // BMP signature
		imageDisplay = new ImageDisplayBMP(m_imgFile,x,y);
	}
	if (signature == 0x4654) { // TFT signature
		imageDisplay = new ImageDisplayTFT(m_imgFile,x,y);
	}
	if (imageDisplay != NULL) {
		goodBmp = imageDisplay->displayFile();
		delete imageDisplay;
		Graphics.setCursor(20, Graphics.height()-10);
		Graphics.print(filename);
	}
	imgFile.close();
	if (!goodBmp) {
		Graphics.setCursor(20, Graphics.height() / 2);
		Graphics.print(PSTR("Image format not recognized: "));
		Graphics.print(filename);
	}
	else {
		Graphics.setCursor(0, Graphics.height());
		Graphics.print(filename);
	}

}

//void Gallery::pushColors(uint16_t *data, uint8_t len, boolean first) {
//	Graphics.pushColors(data, len, first);
//	m_totalLen += len;
//}

Gallery::~Gallery() {
}
//GalleryTFT::~GalleryTFT() {
//}

App *gallery = new Gallery();
//App *galleryTFT = new GalleryTFT();




